package com.ygqh.localsign.utils;

import com.alibaba.fastjson.JSON;
import com.ygqh.customs.domain.PayExInfo;
import com.ygqh.localsign.domain.LocalSignParam;
import com.ygqh.localsign.domain.SignArgs;
import lombok.extern.slf4j.Slf4j;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/21 10:47
 * @since 1.0.0
 */
@Slf4j
public class LocalSignUtil {

    private final static String SIGN_METHOD = "cus-sec_SpcSignDataAsPEM";
    private final static String PASSWORD = "88888888";

    /**
     * 构造字符串
     *
     * @param dataUpSignParam 本地验签参数
     * @return 返回验签字符串
     */
    private static String buildStr(PayExInfo dataUpSignParam) {
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("\"sessionID\":\"").append(dataUpSignParam.getSessionID()).append("\"||");
        stringBuffer.append("\"payExchangeInfoHead\":\"").append(dataUpSignParam.toPayExchangeInfoHeadJSONString()).append("\"||");
        stringBuffer.append("\"payExchangeInfoLists\":\"").append(dataUpSignParam.toPayExchangeInfoListsJSONString()).append("\"||");
        stringBuffer.append("\"serviceTime\":\"").append(dataUpSignParam.getServiceTime()).append("\"");
        return stringBuffer.toString();
    }

    /**
     * 组装本地验签字符串
     *
     * @param dataUpSignParam 数据上传参数
     * @return String
     */
    public static String buildLocalParamStr(PayExInfo dataUpSignParam) {

        LocalSignParam signParam = new LocalSignParam();

        signParam.setMethod(SIGN_METHOD);
        signParam.setId(dataUpSignParam.getDataUpId().toString());


        SignArgs signArgs = new SignArgs();

        String str = buildStr(dataUpSignParam);

        log.info("dataUpSignParam str :{}", str);
        signArgs.setInData(str);
        signArgs.setPasswd(PASSWORD);
        signParam.setArgs(signArgs);

        return JSON.toJSONString(signParam);
    }

}
